﻿/** 
 * Copyright 2010 NUMKIT.COM (contact: opensource@numkit.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author 陈超
 * @version 1.0
 */
import flash.external.ExternalInterface;
class DrawPanel {
	private var aNode:Array;
	private var aLine:Array;
	private var backgroundColor:Number=0x000000;
	private var bkImg:String;
	private var iconSet:IconSet;
	private var rootMC:MovieClip;
	private var tipTF:TextField;
	private var drawCircle:Boolean = false;
	private var drawBkImg:Boolean = false;
	private var textFieldSide:String;
	public function DrawPanel(root:XMLNode, hideMC:MovieClip, onDataLoaded:Function) {
		if (root.nodeName != "chart") {
			throw new Error("Can't find root node chart!");
		}
		aNode = new Array();
		aLine = new Array();
		var circleNodeCount:Number = 0;
		for (var subRoot:XMLNode = root.firstChild; subRoot != null; subRoot=subRoot.nextSibling) {
			if (subRoot.nodeName == "config") {
				for (var child:XMLNode = subRoot.firstChild; child != null; child=child.nextSibling) {
					if(child.nodeName == "nodeTextColor"){
						var temp=Number(child.firstChild.nodeValue);
						if(!isNaN(temp))
							Node.nodeTextColor=temp;
					}else if(child.nodeName == "lineTextColor"){
						var temp=Number(child.firstChild.nodeValue);
						if(!isNaN(temp))
							Line.lineTextColor=temp;
					}else if(child.nodeName == "backgroundColor"){
						var temp=Number(child.firstChild.nodeValue);
						if(!isNaN(temp))
							this.backgroundColor=temp;
					}else if (child.nodeName == "backgroundImage") {
						this.bkImg = child.firstChild.nodeValue;
					} else if (child.nodeName == "icons") {
						iconSet = new IconSet(child, hideMC, onDataLoaded);
					} else if (child.nodeName == "textFieldSide") {
						textFieldSide = child.firstChild.nodeValue;
					}
				}
			} else if (subRoot.nodeName == "nodes") {
				var nodesNode:XMLNode = subRoot;
				for (var child:XMLNode = nodesNode.firstChild; child != null; child=child.nextSibling) {
					var nodeName:String = child.nodeName;
					if (nodeName == "node") {
						var node:Node = new Node(child);
						if (!node.isPosDefined()) {
							circleNodeCount++;
						}
						aNode.push(node);
					}
				}
				//调整位置
				var step:Number = 0;
				if (circleNodeCount>0) {
					step = 2*Math.PI/circleNodeCount;
				}
				var r:Number = ((Stage.width>Stage.height ? Stage.height : Stage.width)-40)/2;
				var j:Number = 0;
				for (var i:Number = 0; i<aNode.length; i++) {
					if (!aNode[i].isPosDefined()) {
						aNode[i].setX(Stage.width/2+r*Math.cos(step*j+Math.PI/2));
						aNode[i].setY(Stage.height/2-r*Math.sin(step*j+Math.PI/2));
						j++;
					}
				}
			} else if (subRoot.nodeName == "lines") {
				var linesNode:XMLNode = subRoot;
				for (var child:XMLNode = linesNode.firstChild; child != null; child=child.nextSibling) {
					var nodeName:String = child.nodeName;
					if (nodeName == "line") {
						var line:Line = new Line(child, aNode);
						line.getNodeA().addLine(line);
						line.getNodeB().addLine(line);
						aLine.push(line);
					}
				}
			}
		}
		var autoSide:Boolean;
		var side:Number;
		if (textFieldSide == "right") {
			autoSide=false;
			side = Node.TEXTSIDE_RIGHT;
		} else if (textFieldSide == "bottom") {
			autoSide=false;
			side = Node.TEXTSIDE_DOWN;
		} else {
			autoSide=true;
		}
		for (var i:Number = 0; i<aNode.length; i++) {
			aNode[i].updateEnds();
			aNode[i].setAutoTextSide(autoSide);
			aNode[i].setTextSide(side);
		}
		if (bkImg != undefined && bkImg != null && bkImg != "") {
			drawBkImg = true;
		}
		if (circleNodeCount>0 && !drawBkImg) {
			drawCircle = true;
		}
		//如果没有指定icon,直接触发数据准备完成事件   
		if (iconSet == undefined) {
			onDataLoaded();
		}
	}
	public function draw(rootMC:MovieClip):Void {
		this.rootMC = rootMC;
		var bkMC:MovieClip = rootMC.createEmptyMovieClip("bkMC", rootMC.getNextHighestDepth());
		bkMC.beginFill(this.backgroundColor, 100);
		bkMC.moveTo(0, 0);
		bkMC.lineTo(0, Stage.height);
		bkMC.lineTo(Stage.width, Stage.height);
		bkMC.lineTo(Stage.width, 0);
		bkMC.lineTo(0, 0);
		bkMC.endFill();
		var r:Number = ((Stage.width>Stage.height ? Stage.height : Stage.width)-40)/2;
		if (drawCircle) {
			rootMC.attachMovie("CycleBorder", "cycleBorder", rootMC.getNextHighestDepth(), {_x:Stage.width/2-r, _y:Stage.height/2-r, _xscale:r*100/230, _yscale:r*100/230});
		}
		if (drawBkImg) {
			var bkImgMC:MovieClip = rootMC.createEmptyMovieClip("bkImgMC", rootMC.getNextHighestDepth());
			//System.security.allowDomain(bkImg);
			var imageLoader:MovieClipLoader = new MovieClipLoader();
			var listener:Object = new Object();
			listener.onLoadInit = function(imageClip:MovieClip):Void  {
				imageClip._xscale = Stage.width*100/imageClip._width;
				imageClip._yscale = Stage.height*100/imageClip._height;
			};
			imageLoader.addListener(listener);
			imageLoader.loadClip(bkImg, bkImgMC);
		}
		var tempMC:MovieClip;
		for (var i:Number = 0; i<aLine.length; i++) {
			tempMC = rootMC.createEmptyMovieClip("line"+i, rootMC.getNextHighestDepth());
			aLine[i].draw(tempMC);
		}
		for (var i:Number = 0; i<aNode.length; i++) {
			tempMC = rootMC.createEmptyMovieClip("node"+i, rootMC.getNextHighestDepth());
			aNode[i].draw(tempMC, iconSet);
		}
		tipTF = rootMC.createTextField("tipTF", rootMC.getNextHighestDepth(), -100, -100, 50, 18);
		tipTF.selectable = false;
		tipTF._visible = false;
		Line.setTipTF(tipTF);
		Mouse.addListener(this);
	}
	public function onMouseWheel(delta:Number):Void {
	}
	public function onMouseDown():Void {
	}
	public function onMouseUp():Void {
	}
	public function onMouseMove():Void {
		if (tipTF != undefined) {
			tipTF._x = rootMC._xmouse;
			tipTF._y = rootMC._ymouse-14;
			tipTF.updateAfterEvent();
		}
	}
}
